package com.chen.recruit.common.api;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 通用返回对象
 *
 * @author cgy
 * @date 2022/09/04
 */
@Getter
@AllArgsConstructor
public class CommonResult {

    @ApiModelProperty("响应码")
    private int code;

    @ApiModelProperty("响应信息")
    private String message;

    @ApiModelProperty("响应数据")
    private Object data;

    public CommonResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 成功返回结果
     */
    public static CommonResult success() {
        return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    /**
     * 成功返回结果(带数据)
     */
    public static CommonResult success(Object data) {
        return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 失败返回结果
     */
    public static CommonResult fail() {
        return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    /**
     * 失败返回结果(带消息)
     */
    public static CommonResult fail(String message) {
        return new CommonResult(ResultCode.FAILED.getCode(), message);
    }

    /**
     * 未授权返回结果
     */
    public static CommonResult forbidden() {
        return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage());
    }

    /**
     * 未登录返回结果
     */
    public static CommonResult unauthorized() {
        return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage());
    }

}
